Questions
22 of 25
1What built-in HTTP exceptions does NestJS provide out of the box?
2What interface must an exception filter implement and what are the two parameters of catch()?
3How do you apply an exception filter at the method, controller, and global level in NestJS?
4How do you add custom response headers or set cookies inside an exception filter in NestJS?
5How do you unit test an exception filter in NestJS?
6How do you implement a filter that adds a request correlation ID to every error response in NestJS?
7How do getResponse() and getStatus() work on an HttpException and what can getResponse() return?
8How do you create a domain-specific custom exception class in NestJS?
9How does exception filter resolution order work when multiple filters are registered at different levels in NestJS?
10How do you register multiple global filters that each handle a different exception type in NestJS?
11What does the @Catch() decorator do and what happens when you omit its argument?
12How do you extend the built-in BaseExceptionFilter to add logging while keeping default NestJS behaviour?
13How do you handle exceptions differently in a filter depending on whether the request is HTTP, WebSocket, or RPC?
14What are the two ways to register a global exception filter in NestJS and what is the key difference?
15How do interceptors and exception filters divide error-handling responsibility in NestJS?
16How do you write an exception filter that handles GraphQL errors differently from REST errors in NestJS?
17How do you prevent sensitive information from leaking through exception responses in production NestJS apps?
18How do you handle async operations inside an exception filter in NestJS, for example logging to a database?
19What is HttpException in NestJS and what two arguments does its constructor take?
20What does NestJS's built-in global exception layer do when no custom filter is registered?
21What is the cause option on HttpException and how should you use it for error tracing?
22How do you write a production-ready global all-exceptions filter that handles HttpException and unexpected errors differently?
23How do you integrate a third-party error tracker like Sentry inside a global exception filter in NestJS?
24How do you map database and ORM exceptions to HTTP exceptions using a global filter in NestJS?
25How do you re-throw an exception from within a filter to let a higher-scoped filter handle it in NestJS?
22 / 25

How do you write a production-ready global all-exceptions filter that handles HttpException and unexpected errors differently?

Use @Catch() with no argument to catch everything. Branch on instanceof HttpException — known HTTP exceptions expose their status and message to the client. Unknown errors are logged in full server-side but return a generic 500 to the client. This two-branch pattern is the production standard.

Production global all-exceptions filter
Production error handling best practices:
  1. 1

    Always log the full error including stack trace for unexpected errors.

  2. 2

    Never expose internal error details, stack traces, or DB errors to the client.

  3. 3

    HTTP exceptions are expected client errors — surface their message and status code.

  4. 4

    Unknown errors should always return 500 with a generic message.

  5. 5

    Include the request path and timestamp in error responses for client-side debugging.

Difficulty: 7/10
Topics: Exception handling, NestJS filters, Error logging

Scenario Questions

0-2 years experience
  1. 1

    Imagine you need to add a global exception filter to a small NestJS service that returns a 400 response for HttpException and a generic 500 for any other error. How would you implement that filter?

  2. 2

    If you forget to call next.handle() inside your filter's catch method, what would happen to the request lifecycle?

  3. 3

    How would you register this filter so that it applies to all controllers without modifying each one?

2-5 years experience
  1. 1

    You added a global filter that logs errors, but now some HttpExceptions are losing their original status codes. What could be causing that and how would you fix it?

  2. 2

    During a load test, you notice the filter is adding significant latency. What trade‑offs would you consider to keep error handling robust yet performant?

  3. 3

    Your team wants the filter to differentiate between client errors (4xx) and server errors (5xx) for monitoring. How would you extend the filter to support that without breaking existing behavior?

5-8 years experience
  1. 1

    In a microservices architecture, several NestJS services share a common library for exception handling. How would you design the global filter to be reusable across services while still allowing each service to inject its own logger or monitoring client?

  2. 2

    When deploying to Kubernetes, you see that unhandled errors sometimes cause the process to crash despite the filter. Explain why that might happen and how you would make the filter truly production‑ready.

  3. 3

    Consider that some HttpExceptions contain sensitive data in their response payload. How would you modify the filter to sanitize responses while still preserving useful debugging info for internal logs?

8+ years experience
  1. 1

    Your organization is migrating legacy Express middleware to NestJS. How would you approach integrating existing error‑handling middleware with a new global NestJS exception filter to ensure a seamless transition?

  2. 2

    Across multiple teams, there’s a need for a standardized error contract (error codes, messages, correlation IDs). How would you evolve the global filter and related infrastructure to enforce this contract at scale?

  3. 3

    If you had to support both synchronous and asynchronous exception sources (e.g., RxJS streams, Promise rejections) in a single filter, what architectural changes would you make to keep the code maintainable and testable?

Follow-up Questions

  • What would you do if you needed to add a correlation ID to every error response?
  • How do you ensure the filter doesn't swallow stack traces needed for debugging?
  • Can you explain how NestJS's built‑in HttpException filter differs from a custom one?